import { defineStore } from "pinia";

export const useUserStore = defineStore("userStore", {
  state: () => ({
    token: null,
    authorityData: [], //权限数据
    roleData: [], //角色数据
    userInfo: null, //用户信息
  }),
  persist: true, // 启用持久化
  getters: {
    tokenGetter: (state) => state.token,
    authorityGetter: (state) => state.authorityData,
    roleGetter: (state) => state.roleData,
    userGetter: (state) => state.userInfo,
  },
  actions: {
    //设置token
    setToken(token: string) {
      this.token = token;
    },
    //清空token
    clearToken() {
      this.token = null;
      this.authorityData.splice(0);
      this.roleData.splice(0);
    },
    //设置权限
    setAuthority(list: any) {
      this.authorityData = list;
    },
    setRole(list: any) {
      this.roleData = list;
    },
    setUser(user: any) {
      this.userInfo = user;
    },
  },
});
